package cn.play.cardgame;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import butterknife.ButterKnife;

public class QCodeDialog extends DialogFragment {
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		return builder.create();
	}

	@Override
	public void onStart() {
		super.onStart();
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		View view = View.inflate(getActivity(), R.layout.fragment_qcode_dialog, null);
		ButterKnife.inject(this, view);
		getDialog().setCancelable(false);
		getDialog().getWindow().setContentView(view);
		getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		
		getDialog().setOnKeyListener(new OnKeyListener() {
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_H:
					if (event.getAction() == KeyEvent.ACTION_UP) {
						((MainActivity) getActivity()).onKeyUp(keyCode, event);
					}
					break;
				}
				return true;
			}
		});
	}
	
	public void dismiss(){
		getDialog().dismiss();
	}
}
